package com.gege;

import com.gege.mapper.DeptMapper;
import com.gege.pojo.Dept;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author gege
 * @date 2021/9/14
 */
public class Test01 {
    public static void main(String[] args) throws IOException {
        //第一步, 应该是获取到mybatis主配置文件
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        //创建一个SqlSession工厂对象
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //创建一个SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //创建代理对象
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
        //调用方法, 去操作数据库
        List<Dept> depts = mapper.selectAll();
        //释放资源
        sqlSession.close();

        for (Dept dept : depts) {
            System.out.println(dept);
        }
    }
}
